package com.ssmm.ssmm1.controller;

import com.ssmm.ssmm1.mapper.UserrMapper;
import com.ssmm.ssmm1.pojo.Userandrole;
import com.ssmm.ssmm1.pojo.Userr;
import com.ssmm.ssmm1.service.UserService;
import com.ssmm.ssmm1.service.UserServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by naojiang on 2017/7/12.
 */

@Controller
@RequestMapping("/user")
@Scope("prototype")
@Resource(name = "UserService")
public class UserController {
    //自动注入业务层的userService类
    @Autowired
    public UserService userService;

    @RequestMapping(value = {"/login"},method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    //login业务的访问位置为/user/login
    @RequestMapping(value = {"/login"},method = RequestMethod.POST)
    public String login(Userr userr, HttpSession session, Userandrole userandrole, HttpServletRequest request){
        //调用login方法来验证是否是注册用户
        userandrole.setRole(request.getParameter("name"));
        int loginType = userService.login(userr.getUsername(),userr.getPassword(),userandrole.getRole());
        ModelMap modelMap = new ModelMap();
        ModelAndView modelAndView;
        if(loginType==1){
            //如果验证通过,则将用户信息传到前台
            String message = "登录";
            List<Userr> list = userService.list();
            session.setAttribute("message",message);
            session.setAttribute("list1",list);
            return "list";
        }else if(loginType==2){
            List<Userr> list1 = userService.list();
            Userr userr1 = userService.find(userr.getUsername());
            session.setAttribute("list1",list1);
            session.setAttribute("user1",userr1);
            return "normallist";
        }else{
            String message = "登录";
            session.setAttribute("message",message);
            return "error";
        }
    }

    @RequestMapping(value = {"/register"},method = RequestMethod.GET)
    public String register(){
        return "register";
    }

    @RequestMapping(value = {"/register"},method = RequestMethod.POST)
    public String register(Userr userr, HttpSession session){
        //注册
        System.out.println(userr.getUsername()+" "+userr.getPassword()+" "+userr.getNickname()
                +" "+userr.getSex()+" "+userr.getAddress());
        boolean registerType = userService.register(userr.getUsername(),userr.getPassword(),userr.getNickname()
                                                    ,userr.getSex(),userr.getAddress());
        ModelMap modelMap = new ModelMap();
        ModelAndView modelAndView;
        if(registerType){
            List<Userr> list1 = userService.list();
            Userr userr1 = userService.find(userr.getUsername());
            session.setAttribute("list1",list1);
            session.setAttribute("user1",userr1);
            return "normallist";
        }else{
            String message = "注册";
            session.setAttribute("message",message);
            return "error";
        }
    }

    @RequestMapping(value = {"/delete"},method = RequestMethod.GET)
    public String delete(Userr userr,HttpServletRequest request,HttpSession session){
        String username = (String) request.getParameter("username");
        System.out.println(username);
        boolean deleteType = userService.delete(username);
        List<Userr> list = userService.list();
        session.setAttribute("list1",list);
        if(deleteType){
            return "list";
        }else{
            return "error";
        }
    }

    @RequestMapping(value = {"/update"},method = RequestMethod.GET)
    public String update(Userr userr,HttpServletRequest request,HttpSession session){

        String username = (String) request.getParameter("username");
        System.out.println(username);
        session.setAttribute("username",username);

        return "update";
    }

    @RequestMapping(value = {"/update"},method = RequestMethod.POST)
    public String update(Userr userr,HttpSession session){
        System.out.println(userr.getUsername()+" "+userr.getPassword()+" "+userr.getNickname()
                +" "+userr.getSex()+" "+userr.getAddress());
        boolean updateType = userService.update(userr.getUsername(),userr.getPassword(),userr.getNickname()
                ,userr.getSex(),userr.getAddress());
        ModelMap modelMap = new ModelMap();
        ModelAndView modelAndView;
        if(updateType){
            String message = "修改";
            List<Userr> list = userService.list();
            session.setAttribute("message",message);
            session.setAttribute("list1",list);
            return "list";
        }else{
            String message = "修改";
            session.setAttribute("message",message);
            return "error";
        }
    }

    @RequestMapping(value = {"/updatenormal"},method = RequestMethod.GET)
    public String updatenormal(Userr userr,HttpServletRequest request,HttpSession session){

        String username = (String) request.getParameter("username");
        System.out.println(username);
        session.setAttribute("username",username);

        return "normalupdate";
    }

    @RequestMapping(value = {"/updatenormal"},method = RequestMethod.POST)
    public String updatenormal(Userr userr,HttpSession session){
        System.out.println(userr.getUsername()+" "+userr.getPassword()+" "+userr.getNickname()
                +" "+userr.getSex()+" "+userr.getAddress());
        boolean updateType = userService.update(userr.getUsername(),userr.getPassword(),userr.getNickname()
                ,userr.getSex(),userr.getAddress());
        ModelMap modelMap = new ModelMap();
        ModelAndView modelAndView;
        if(updateType){
            List<Userr> list1 = userService.list();
            Userr userr1 = userService.find(userr.getUsername());
            session.setAttribute("list1",list1);
            session.setAttribute("user1",userr1);
            return "normallist";
        }else{
            String message = "修改";
            session.setAttribute("message",message);
            return "error";
        }
    }

    @RequestMapping(value = {"/list"},method = RequestMethod.POST)
    public String list(Userr userr,HttpServletRequest request,HttpSession session){

        System.out.println(userr.getUsername());
        List<Userr> list2 = userService.listPart(userr.getUsername());
        session.setAttribute("list1",list2);
        return "list";
    }

}
